package com.chinasoft.news.controller;

import com.chinasoft.news.customexception.BussinessException;
import com.chinasoft.news.pojo.ExType;
import com.chinasoft.news.pojo.Result;
import com.chinasoft.news.pojo.User;
import com.chinasoft.news.service.UserService;
import com.chinasoft.news.utils.JwtUtils;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@AllArgsConstructor
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;

//    @GetMapping("/my/userinfo")
//    public Result getUserInfo(@RequestHeader("Authorization") String token){
//        Result result = new Result(0, "userinfo is ok");
//        System.out.println(token);
//        boolean b = JwtUtils.verifyToken(token);
////        if(!b){
////            result.setStatus(4);
////            result.setMessage("invaild token");
////            return result;
////        }
//        User userInfo = userService.getUserInfo(token);
//        userInfo.setPassword("");
//        result.setData(userInfo);
//        return result;
//    }

    @PostMapping("/my/userinfo")
    public Result updateInfo(@RequestParam("nickname") String nickname,@RequestParam("email") String email ,@RequestParam("id") Integer id){
        Result result = new Result(0, "update userinfo successful");
        Boolean updateInfo = userService.updateInfo(nickname, email, id);
        if(!updateInfo){
            result.setStatus(1);
            result.setMessage("用户信息更新失败");
        }
        return  result;
    }

    @PostMapping("/my/updatepwd")
    public Result updatePwd(HttpServletRequest request,@RequestParam("oldPwd") String oldPassword,@RequestParam("newPwd")  String newPassword){
        Result result = new Result(0, "update successful");
        Integer userId = (Integer) request.getAttribute("userId");
        if(newPassword.equals(oldPassword)){
            result.setStatus(1);
            result.setMessage("新密码与旧密码相同，更新失败");
        }

        String password = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        System.out.println(password);
        Boolean update = userService.update(password,userId);
        if(!update){
            result.setStatus(1);
            result.setMessage("更新密码失败");
        }

        return  result;
    }

    @GetMapping("/my/userinfo")
    public Result getUserInfo(HttpServletRequest request){
        Result result = new Result(0, "userinfo is ok");
//        System.out.println(token);
//        boolean b = JwtUtils.verifyToken(token);
//        if(!b){
//            result.setStatus(4);
//            result.setMessage("invaild token");
//            return result;
//        }
        Integer userId = (Integer) request.getAttribute("userId");
        User userInfo1 = userService.getUserInfo1(userId);
        userInfo1.setPassword("");
        result.setData(userInfo1);
        return result;
    }


    @PostMapping("/my/update/avatar")
    public Result updateUserAvatar(HttpServletRequest request,@RequestParam("avatar") String avatar){
        Result result = new Result(0, "update avatar is ok");
        Integer userId = (Integer) request.getAttribute("userId");
        Boolean updateAvatar = userService.updateAvatar(avatar, userId);
        if(!updateAvatar){
            result.setStatus(1);
            result.setMessage("用户头像更新失败");
        }
        User user = new User();
        user.setUser_pic(avatar);
        result.setData(user);
        return result;

    }



}
